package com.example.myapplication.ui.Home.fragments;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;

import com.example.myapplication.R;
import com.example.myapplication.base.BaseMvpFragment;
import com.example.myapplication.model.bean.DownListBean;
import com.example.myapplication.model.bean.DownloadData;
import com.example.myapplication.model.buffr.DownInfo;
import com.example.myapplication.model.buffr.DownResultListenner;
import com.example.myapplication.model.buffr.DownState;
import com.example.myapplication.model.https.RetrofitDownloadManager;
import com.example.myapplication.presenter.DownloadPresenter;
import com.example.myapplication.presenter.contract.DownloadContract;
import com.example.myapplication.ui.Home.adapter.ContentAdapter;
import com.example.myapplication.ui.wight.OnClickListener;
import com.example.myapplication.utils.NetworkUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class DownloadFragment extends BaseMvpFragment<DownloadPresenter> implements DownloadContract.View, DownResultListenner, OnClickListener {
    @BindView(R.id.rc_view)
     RecyclerView rc_view;
    ContentAdapter adapter;
    private RetrofitDownloadManager mRetrofitDownloadManager;
    private ProgressBar progressBar;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_download;
    }

    @Override
    protected void initView() {
            mRetrofitDownloadManager = new RetrofitDownloadManager(this);
        rc_view.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter=new ContentAdapter(R.layout.fragment_download_list_item,new ArrayList<>(),this);
        rc_view.setAdapter(adapter);
        }


    @Override
        public void showError(String msg) {

        }


    @Override
    protected DownloadPresenter initPresenter() {
        return new DownloadPresenter(getActivity(),mRetrofitDownloadManager);
    }

    @Override
    protected void initEventAndData() {
        mPresenter.requestSplashData(1,1);
    }

    @Override
    public void scouss(DownListBean downListBean) {
       List<DownloadData> list = localList(downListBean.getList());
        adapter.addData(list);
    }
    /** 本地有缓存，那么直接从本地取，不重新下载
     * @param list*/
    public List<DownloadData> localList(List<DownListBean.ListBean> list){
        //保存路径
        String savePath = NetworkUtil.getInstance().getDownloadDirectory().getAbsolutePath();
        //过滤，如果本地有下载过同URL。直接本地拿数据
        List<DownloadData> resultList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            DownListBean.ListBean bean = list.get(i);
            String downUrl = bean.getDown_url();
            //创建下载类，有判断缓存
            DownInfo downInfo = mRetrofitDownloadManager.createDownInfo(downUrl,savePath);
            //转换类，进行下载
            DownloadData downloadData = new DownloadData(bean.getLogo_url(), bean.getName(), bean.getVersion_name(), bean.getSize_fixed(),downInfo);
            resultList.add(downloadData);
        }
        return resultList;
    }

    @Override
    public void updateProgress(DownInfo info, int progress) {
        DownloadData downloadData = queryDownloadData(info);
        if(downloadData != null){
            //LogUtils.logi("updateProgress -》" + progress + " %");
            //databind 内部利用 notifyPropertyChanged 刷新布局
            //如果你不是用的 dataBind  那么你自己要在这边刷新布局显示咯
            downloadData.updateProgress(DownState.DOWN);
          //  adapter.getData().add(mpostion,downloadData);
            adapter. notifyDataSetChanged();


        }
    }

    @Override
    public void updateState(DownInfo info, DownState state) {
        DownloadData downloadData = queryDownloadData(info);
        if(downloadData != null){
            //databind 内部利用 notifyPropertyChanged 刷新布局
            //如果你不是用的 dataBind  那么你自己要在这边刷新布局显示咯
            downloadData.updateProgress(state);
        }
    }
    /** 查找 下载的对象对应List 列表的哪个数据，并刷新 */
    private DownloadData queryDownloadData(DownInfo info){
        List<DownloadData> data = adapter.getData();
        for (int i = 0; i < data.size(); i++) {
            DownInfo downInfo = data.get(i).getDownInfo();
            if(downInfo == info){
                return data.get(i);
            }
        }
        return null;
    }
    int mpostion;
    @Override
    public void onClick(DownloadData data, int postion, View view) {
        DownInfo downInfo = data.getDownInfo();
        mRetrofitDownloadManager.down(downInfo);
        mpostion=postion;
       // adapter.notifyItemChanged(postion);


    //    progressBar= (ProgressBar) view.findViewById(R.id.progressBar);
    }


}
